package com.leador.socket;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.io.DataInputStream;

/**
 * Created by liuha on 2016/9/14.
 */
public class ReadThread extends Thread{
    private boolean readEnable = true;
    private SocketMananger socketMananger = null;
    private DataInputStream dataInputStream = null;
    public ReadThread(SocketMananger socketMananger){
        this.socketMananger = socketMananger;
    }
    @Override
    public void run() {
        while (readEnable){
            try {
                dataInputStream = socketMananger.getDataInputStream();
                //(!ParseJson.writeInputStream(this.dataInputStream)) 该地方有推送和验证等信息
                if(null != dataInputStream){
                    throw new Exception();
                }
            } catch (Exception e) {
                Log.e("read err","read err");
                e.printStackTrace();
                if(!readEnable){
                    return;
                }
                readEnable = false;
                Handler handler = socketMananger.getHandler();
                if(null != handler){
                    Message message = handler.obtainMessage(4);
                    message.sendToTarget();
                }
            }
        }
    }

    public void setReadEnable(boolean readEnable) {
        this.readEnable = readEnable;
    }
}
